﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DotNetExtensions.AspNet.SQLites
{
    public class TableInfoFactory : Singleton<TableInfoFactory>
    {
        public readonly ConcurrentDictionary<string, TableInfo> tableInfos;
        private TableInfoFactory()
        {
            tableInfos = new ConcurrentDictionary<string, TableInfo>();
        }

        public TableInfo Get(Type type)
        {
            TableInfo tableInfo;
            if (tableInfos.TryGetValue(type.FullName, out tableInfo))
            {
                return tableInfo;
            }

            tableInfo = new TableInfo(type);
            tableInfos.TryAdd(type.FullName, tableInfo);

            return tableInfo;
        }

        public TableInfo Get<T>()
        {
            Type type = typeof(T);
            TableInfo tableInfo;
            if (tableInfos.TryGetValue(type.FullName, out tableInfo)) return tableInfo;

            tableInfo = new TableInfo(type);
            tableInfos.TryAdd(type.FullName, tableInfo);

            return tableInfo;
        }
    }

}
